package com.khbr.employee_imfo.controller;
import com.khbr.employee_imfo.entity.Employee;
import com.khbr.employee_imfo.model.Condition;
import com.khbr.employee_imfo.model.PageDepart;
import com.khbr.employee_imfo.service.IEmployeeService;
import com.khbr.employee_imfo.service.impl.EmployeeService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
/**
 * Created by John.C on 2018/11/29.
 */
public class EmployeeControl extends HttpServlet {
    IEmployeeService employeeService=new EmployeeService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String uri = req.getRequestURI();
        System.out.println(uri);
        //根据资源的路径返回页面
        if (uri.contains("list")) {
            listPage(req,resp);
        } else if (uri.contains("detail")) {
            detailPage(req,resp);
        } else if(uri.contains("toadd")){
            getAddpage(req,resp);
        } else if(uri.contains("add")&&"POST".equals(req.getMethod().toUpperCase())){
            try{
                addPage(req,resp);
            }catch (ParseException pE){
                pE.printStackTrace();
            }
        } else if(uri.contains("delete")){
            deletePage(req,resp);
        } else if(uri.contains("toupdate")){
            getUpdatePage(req,resp);
        } else if(uri.contains("update")&&"POST".equals(req.getMethod().toUpperCase())){
            try{
                updatePage(req,resp);
            }catch(ParseException pE){
                pE.printStackTrace();
            }
        }
    }

    public void listPage(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        String number=req.getParameter("number");
        String name=req.getParameter("name");
        Condition condition = new Condition();
        if(number!=null&&!"".equals(number)){
            condition.setNumber(number.trim());
        }
        if(name!=null&&!"".equals(name)){
            condition.setName(name.trim());
        }
        String curntPg=req.getParameter("curntPg");
        String count=req.getParameter("count");
        PageDepart pageDepart=new PageDepart();
        if(curntPg!=null&&!"".equals(curntPg)){
            pageDepart.setHead(Integer.parseInt(curntPg));
        }
        if(count!=null&&!"".equals(count)){
            pageDepart.setCount(Integer.parseInt(count));
        }
        List<Employee> list=employeeService.multipleFind(pageDepart,condition);
        int record=employeeService.count(condition);
        pageDepart.setRecord(record);
        req.setAttribute("stuffPage",list);
        req.setAttribute("find",condition);
        req.setAttribute("page",pageDepart);
        //返回ticketList.jsp
        req.getRequestDispatcher("/WEB-INF/view/stuffPage.jsp").forward(req,resp);
    }

    public void detailPage(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        String id=req.getParameter("id");
        Employee employee=employeeService.find(Integer.parseInt(id));
        req.setAttribute("detail",employee);
        req.getRequestDispatcher("/WEB-INF/view/detail.jsp").forward(req,resp);
    }

    public void getAddpage(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        req.getRequestDispatcher("/WEB-INF/view/add.jsp").forward(req,resp);
    }

    public void addPage(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException,ParseException{
        //获取参数
        String number=req.getParameter("number");
        String name=req.getParameter("name");
        String gender=req.getParameter("gender");
        String age=req.getParameter("age");
        String jobType=req.getParameter("jobType");
        String entryTime=req.getParameter("entryTime");
        Employee employee=new Employee();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        employee.setNumber(number);
        employee.setName(name);
        employee.setGender(gender);
        employee.setAge(Integer.parseInt(age));
        employee.setJobType(jobType);
        employee.setEntryTime(new Date(format.parse(entryTime).getTime()));
        employeeService.create(employee);
        resp.sendRedirect("/entrance/list");
    }

    public void deletePage(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        String id=req.getParameter("id");
        employeeService.delete(Integer.parseInt(id));
        resp.sendRedirect("/entrance/list");
    }

    public void getUpdatePage(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        String id=req.getParameter("id");
        Employee employee=employeeService.find(Integer.parseInt(id));
        req.setAttribute("data",employee);
        req.getRequestDispatcher("/WEB-INF/view/update.jsp").forward(req,resp);
    }

    public void updatePage(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException,ParseException{
        String number = req.getParameter("number");
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        String age=req.getParameter("age");
        String jobType = req.getParameter("jobType");
        String entryTime = req.getParameter("entryTime");
        String id=req.getParameter("id");

        Employee employee=new Employee();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");

        employee.setNumber(number);
        employee.setName(name);
        employee.setGender(gender);
        employee.setAge(Integer.parseInt(age));
        employee.setJobType(jobType);
        employee.setEntryTime(new Date(format.parse(entryTime).getTime()));
        employee.setId(Integer.parseInt(id));
        employeeService.update(employee);
        resp.sendRedirect("/entrance/list");
    }
}